package cn.admin.controller;

import cn.admin.config.key.RequestKey;
import cn.admin.config.key.SessionKey;
import cn.admin.entity.Admin;
import cn.admin.entity.Menus;
import cn.admin.entity.User;
import cn.admin.service.MenusService;
import cn.admin.service.QuotaService;
import cn.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 14:26 2019/3/23
 */
@Controller
public class IndexController {

    @Autowired private UserService userService;
    @Autowired private QuotaService quotaService;
    @Autowired private MenusService menusService;


    /**
     * 首页
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpSession session){
        Admin admin = (Admin) session.getAttribute(SessionKey.SESSION_NAME_ADMIN);
        List<Menus> menus = menusService.getMenus(admin.getAdmin_id());
        request.setAttribute("menus",menus);
        return "/index";
    }

    /**
     * 真正的首页
     * @return
     */
    @RequestMapping("/index_content")
    public String indexContent(HttpServletRequest request){
        int registerTodayUsersCount = userService.getRegisterTodayUsersCount();
        request.setAttribute(RequestKey.REQUEST_REGISTER_TODAY_USERS_COUNT,registerTodayUsersCount);

        int  numberOfAuthenticatedUsers= userService.getNumberOfAuthenticatedUsers();
        request.setAttribute(RequestKey.REQUEST_NUMBER_OF_AUTHENTICATED_USERS,numberOfAuthenticatedUsers);

        String allQuota = quotaService.getAllQuota();
        request.setAttribute(RequestKey.REQUEST_ALL_QUOTA,allQuota);

        List<User> users = userService.getUsers();  //得到所有用户
        request.setAttribute(RequestKey.REQUEST_USERS,users);

        String totalLoanAmount = quotaService.getTotalLoanAmount();
        request.setAttribute(RequestKey.REQUEST_TOTAL_LOAN_AMOUNT,totalLoanAmount);
        return "/index_content";
    }


}
